package org.microservice.auth.Intercept;

import org.apache.http.HttpStatus;
import org.microservice.common.util.constant.CacheConstant;
import org.microservice.common.util.constant.MqQueueConstant;
import org.microservice.redis.util.RedisService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/8/13
 * Time: 13:37
 * To change this template use File | Settings | File Templates.
 * Description:  preHandler -> Controller -> postHandler -> model渲染-> afterCompletion
 */
@Component
public class LoginHandlerInterceptor extends HandlerInterceptorAdapter{


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisService redisService;






    /**
     * /*该方法将在请求处理之前进行调用，只有该方法返回true，才会继续执行后续的Interceptor和Controller，当返回值为true 时就会继续调用下一个Interceptor的preHandle 方法，
     * 如果已经是最后一个Interceptor的时候就会是调用当前请求的Controller方法
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("==============请求之前======================");
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        System.out.println(parameterMap.toString());
        return true;
    }

    /**
     * /*该方法将在请求处理之后，DispatcherServlet进行视图返回渲染之前进行调用，
     * 可以在这个方法中对Controller 处理之后的ModelAndView 对象进行操作。
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("请求之后");
        httpServletResponse.getStatus();


    }

    /**
     * /*该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行，该方法将在整个请求结束之后，
     * 也就是在DispatcherServlet 渲染了对应的视图之后执行。用于进行资源清理。
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        if(HttpStatus.SC_OK == httpServletResponse.getStatus()){
            String username = httpServletRequest.getParameter("username");
            if(!redisService.exists(CacheConstant.USER_INFO_KEY+ username)) {
                rabbitTemplate.convertAndSend(MqQueueConstant.USER_DATA_SCOPE_CHANGE, username);
            }

        }
    }
}
